package com.sunclouder.cactus.common;


import com.sunclouder.cactus.common.constant.LanguageConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @ClassName MessageSourceAware
 * @Desc 国际化处理的异常
 * @Author yan.zhao
 * @Date 2023/9/23
 */
@Component
@Slf4j
public class MessageSourceAware implements ApplicationContextAware {

    private static MessageSource messageSource;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        messageSource = applicationContext.getBean(MessageSource.class);
    }

    public static String getOrDefault(String msgKey, String defaultMessage) {
        try {
            return get(msgKey);
        } catch (Exception e) {
            return defaultMessage;
        }
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey) {
        try {
            Locale var3 = LocaleContextHolder.getLocale();
            return messageSource.getMessage(msgKey, null, var3);
        } catch (Exception e) {
            return messageSource.getMessage(msgKey, null, Locale.getDefault());
        }
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String getSingleByConfig(long msgKey, String languageConfig) {
        String messageCode = String.valueOf(msgKey);
        try {
            // 获取配置
            Locale var3 = getLocaleByConfig(languageConfig);
            String result = messageSource.getMessage(messageCode, null, var3);
            return result;
        } catch (Exception e) {
            log.error("get single message error,msgKey:{}, languageConfig:{}", msgKey, languageConfig);
            return messageSource.getMessage(messageCode, null, Locale.getDefault());
        }
    }

    /**
     * 获取多个国际化翻译值
     */
    public static String[] getMoreByConfig(List<Long> msgKeys) {
        // 获取配置
        Locale var3 = LocaleContextHolder.getLocale();
        List<String> messages = new ArrayList<>();
        Locale finalVar = var3;

        msgKeys.forEach(obj -> {
            String message = "";
            String messageCode = String.valueOf(obj);
            try {
                message = messageSource.getMessage(messageCode, null, finalVar);
            } catch (Exception e) {
                message = messageSource.getMessage(messageCode, null, Locale.getDefault());
            }
            messages.add(message);
        });
        return messages.stream().toArray(String[]::new);
    }

    /**
     * 获取一个一一对应的 Map
     */
    public static Map<Long, String> getMoreForMap(List<Long> msgKeys) {
        // 获取配置
        Locale var3 = LocaleContextHolder.getLocale();
        Map<Long, String> messageMap = new HashMap<>();
        Locale finalVar = var3;

        msgKeys.forEach(obj -> {
            String message = "";
            String messageCode = String.valueOf(obj);
            try {
                message = messageSource.getMessage(messageCode, null, finalVar);
            } catch (Exception e) {
                message = messageSource.getMessage(messageCode, null, Locale.getDefault());
            }
            messageMap.put(obj, message);
        });
        return messageMap;
    }

    /**
     * 获取当前配置。
     *
     * @return
     */
    private static Locale getLocaleByConfig(String languageConfig) {
        Locale var3;
        if (LanguageConstant.EN.equals(languageConfig)) {
            var3 = Locale.US;
        } else {
            var3 = Locale.CHINA;
        }
        return var3;
    }

}
